package com.franklions.nacos.example.controller;

import com.alibaba.nacos.api.config.annotation.NacosValue;
import com.franklions.nacos.example.config.AppConfigProperties;
import com.franklions.nacos.example.config.AppConfigPropertiesBean;
import com.franklions.nacos.example.service.ConfigServiceDemo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import static org.springframework.web.bind.annotation.RequestMethod.GET;

@Controller
@RequestMapping("config")
public class ConfigController {

    @Autowired
    private AppConfigProperties properties;

    @Autowired
    private ConfigServiceDemo configServiceDemo;

    @Autowired
    private AppConfigPropertiesBean appConfigPropertiesBean;

    @NacosValue(value = "${useLocalCache:false}", autoRefreshed = true)
    private boolean useLocalCache;

    @RequestMapping(value = "/get", method = GET)
    @ResponseBody
    public boolean get() {
        return useLocalCache;
    }

    @GetMapping("/properties")
    @ResponseBody
    public Object getProperties(){
        return properties.getName();
    }

    @GetMapping("/text")
    @ResponseBody
    public String getContext(){
        return configServiceDemo.demoGetConfig("nacos-example-app","DEFAULT_GROUP");
    }

    @GetMapping("/bean")
    @ResponseBody
    public AppConfigPropertiesBean getBean(){
        return appConfigPropertiesBean;
    }
}